import { BUNDLE_TYPE_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import LevelBase from "../../LevelBase";
import LvItem from "../../LvItem";
import { AudioManager1 } from "../../manager/AudioManager1";
import { UIManager } from "../../manager/UIManager";
import Level_23_Base from "./Level_23_Base";
import LevelPre_23 from "./LevelPre_23";
const { ccclass, property } = cc._decorator;

let ANSWER: Array<Array<[string, boolean, string, string]>> = [];
let QUESION = [];

let WRONG_NUM = 3;

enum GAME_STATE {
    RUNNING, WIN, FAIL
}

@ccclass
export default class Level_23_24 extends Level_23_Base {
    levelBase: LevelBase = null
    m: number = 0;
    /** 更新选择 */
    gengxinxuanze = true;
    panduan = false;
    gN(nodeName) {
        return this.levelPre.getChildForName(nodeName)
    }

    start() {
        this.levelPre = cc.find("Canvas").getComponentInChildren(LevelPre_23);
        super.start();
        this.levelPre.node.on(cc.Node.EventType.TOUCH_START, this.npcTalkEnd, this)
        let isAutoAnswerActive = Tools.getStorage('setAutoAnswerActive')
        if (isAutoAnswerActive && isAutoAnswerActive != "false") {
            this.autoAnswer = true;
        } else {
            this.autoAnswer = false;
        }

        let isAnswerRightActive = Tools.getStorage('setAnswerRightActive')
        if (isAnswerRightActive && isAnswerRightActive != "false") {
            this.answerRight = true;
        } else {
            this.answerRight = false;
        }

        // 适配
        let widthScale = this.levelPre.maskNode.width / this.levelPre.huaNode.width;
        let heightScale = this.levelPre.maskNode.height / this.levelPre.huaNode.height;
        this.levelPre.huaNode.scale = Math.min(widthScale, heightScale);

        this.levelPre.tipLabNode.y += 100;

    }
    initGame() {

        this.scheduleOnce(() => {
            super.initGame();
        }, 1)

    }
    getDelayTime(isRight: boolean) {
        // if (this.levelPre.progress == 8) {
        //     return 5.5
        // } else {
        return isRight ? 1 : 0.2;
        // }
    }

    resetAnim() {
        this.mainSpine.node.active = true;
        // if (this.mainSpine.animation != "daiji") this.mainSpine.setAnimation(0, "pa", true);
        // if (this.npcSpine.animation != "daiji") this.npcSpine.setAnimation(0, "animation", true);
    }
    talkAnim() {
        // cc.log("talkAnim",this.levelPre.progress)
        // if(this.levelPre.progress == 9){
        //     this.gN("qiaomen").active = true;
        //     this.scheduleOnce(()=>{
        //         this.gN("qiaomen").active = false;
        //     },4)
        // }

    }
    npcTalkEnd() {
        if (this.gengxinxuanze) {
            return;
        }
        console.log("progress", this.levelPre.progress);

        // if (this.levelPre.progress == 0) {
        //     let sayInfo1 = { "str": "内心话:落花村不是一千年前就被灭了吗，难道她......", "audio": { "resUrl": "落花村不是一千年前就被灭了吗，难道她......", "value": 1 } }
        //     this.levelPre.showTipInfo(sayInfo1);
        // }

        this.gengxinxuanze = true
        this.levelPre.content.active = true;
        this.updateOption();
        // super.npcTalkEnd();
        this.npcTalk.active = true;
        this.npcTalk.getComponent(LvItem).setLabString(QUESION[this.levelPre.progress][0]);
        cc.Tween.stopAllByTarget(this.npcTalk);
        AudioManager1.stopAudio(this.NpcAudioId)
        UIManager.ins.enabledGameClick(true);
    }
    protected onDisable(): void {
        this.node.off(cc.Node.EventType.TOUCH_END, this.npcTalkEnd, this)
    }
    rightAnim() {
    }
    async wrongAnim() {
    }
    finish(finishStr: any): void {
        this.npcTalk.active = false;

        this.levelPre = cc.find("Canvas").getComponentInChildren(LevelPre_23);

        ANSWER = this.levelPre.interJson.answer;
        QUESION = this.levelPre.interJson.questions;
        WRONG_NUM = this.levelPre.interJson.wrongNum;

        // super.finish(finishStr);
        if (typeof finishStr != "string") {
            return;
        }
        if (finishStr.includes("ans")) {
            this.isSelect = true;
            let progress = Number(finishStr.split("_")[2]);
            let answer = Number(finishStr.split("_")[3]);
            this.levelPre.playQuestionAnim(false);
            this.checkAnswer(progress, answer - 1);
        }
        if (finishStr === "initGame") {
            this.initGame();

        }
        if (finishStr === "panduanTrue") {
            this.panduan = true;
        }
        if (finishStr === "yincangxuanxiang") {
            this.levelPre.playQuestionAnim(false);
        }
        if (finishStr === "panduan") {
            this.panduan = false;
            this.scheduleOnce(() => {
                if (this.panduan) {

                } else {
                    this.levelPre.progress++
                    this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("overGame").interTween);
                }
            }, 5)

        }
        if (finishStr === "reviveGame") {
            // AudioManager.playBG("恐怖背景音乐", 0.4, BUNDLE_TYPE_ENUM.LEVEL);

            this.gN("失败突脸背景图").active = false;
            this.gN("YinXiang").active = false;
            this.mainSpine.animation = "daiji";
            this.levelPre.progress--;
            this.wrongNum--;
            this.resetAnim();
            this.startGame();
        }
    }

    /** 检测答案对错 */
    async checkAnswer(progress: number, answer: number) {
        UIManager.ins.enabledGameClick(false);
        if (this.levelPre.progress == 0) {
            this.gN("ai").active = false;
            this.levelPre.tipLabNode.active = false
            AudioManager1.stopAudioForName(this.levelPre.lastAudio);
        }

        AudioManager1.stopAudio(this.NpcAudioId)
        let questions = ANSWER[progress];
        let txt = questions[answer][3] || questions[answer][0];
        let isRight = questions[answer][1];
        if ((this.autoAnswer && this.levelPre.progress != 7) || (this.answerRight && this.levelPre.progress != 7)) {
            isRight = true;
        }
        let audio = questions[answer][2];
        if (isRight === true && audio === "1") {
            AudioManager1.playAudio("xuandui", 1, BUNDLE_TYPE_ENUM.LEVEL);
        }
        let audioUrl = (progress + 1) + "" + audio;
        this.mainTalk.active = true;
        this.mainTalk.getComponent(LvItem).setLabString(txt);
        cc.Tween.stopAllByTarget(this.mainTalk);
        this.mainSpine.setAnimation(0, "daiji_sh", true);

        this.MainAudioId = await AudioManager1.playAudio(audioUrl, 1, BUNDLE_TYPE_ENUM.LEVEL);
        let audioTime = cc.audioEngine.getDuration(this.MainAudioId);

        let time = this.getDelayTime(isRight);
        cc.tween(this.node).delay(audioTime).call(() => {
            this.mainSpine.setAnimation(0, "daiji", true);
            if (!isRight) this.wrongNum += 1;
            this.levelPre.progress++;
            this.updateGameState();
            // cc.log("this.gameState == " + this.gameState, this.wrongNum)

            UIManager.ins.enabledGameClick(true);
            this.npcTalk.active = false;
            this.mainTalk.active = false
            if (isRight) {
                this.rightAnim();
            } else {
                this.subHp();
                this.wrongAnim();
            }
        }).delay(time).call(() => {
            this.updateGame();
        }).start();
    }
    updateGame() {
        switch (this.gameState) {
            case GAME_STATE.RUNNING:
                this.resetAnim();
                this.startGame()
                break;
            case GAME_STATE.WIN:
                // this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("winGame").interTween);
                this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("overGame").interTween);
                break;
            case GAME_STATE.FAIL:

                switch (this.levelPre.progress) {
                    case 2:
                    case 3:
                    case 5:
                        this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("overGame2").interTween);
                        break;
                    default:
                        this.levelPre.runNodeTween(this.node, this.levelPre.getInterTwForName("overGame").interTween);
                        break;
                }
                break;
            default:
                break;
        }
    }
    /** 游戏状态 */
    updateGameState() {
        if (this.wrongNum >= WRONG_NUM) {
            this.gameState = GAME_STATE.FAIL;
        } else if (this.levelPre.progress === ANSWER.length) {
            this.gameState = GAME_STATE.WIN;
        } else {
            this.gameState = GAME_STATE.RUNNING;
        }
    }

    /** 开始一轮游戏 */
    async startGame() {
        this.isSelect = false;
        cc.log("当前第" + this.levelPre.progress + "题")
        this.gengxinxuanze = false
        UIManager.ins.enabledGameClick(true);
        this.npcTalk.active = true;
        this.npcTalk.getComponent(LvItem).setLabString(QUESION[this.levelPre.progress]);

        if (this.levelPre.progress == 0) {
            cc.tween(this.gN("ai")).call(() => { this.gN("ai").active = true }).to(0.35, { opacity: 255 }).start();
        }

        this.talkAnim();
        this.NpcAudioId = await AudioManager1.playAudio("" + (1 + this.levelPre.progress), 1, BUNDLE_TYPE_ENUM.LEVEL);
        cc.Tween.stopAllByTarget(this.npcTalk);
        let audioTime = cc.audioEngine.getDuration(this.NpcAudioId);
        cc.tween(this.npcTalk).delay(audioTime).call(() => this.npcTalkEnd()).start();


        if (this.levelPre.progress == 8) {
            this.gN("YinXiang").active = true;
        }
    }

}